package cn.itcast.service.cargo.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ExtCproduct extc) {
        // 1.设置附件主键id
        extc.setId(IdUtil.simpleUUID());
        // 2.计算并设置附件总金额
        double money = 0d;
        if (extc.getPrice() != null && extc.getCnumber() != null) {
            money = extc.getPrice() * extc.getCnumber();
        }
        extc.setAmount(money);
        // 3.保存附件对象
        extCproductDao.insertSelective(extc);
        // 4.根据合同id，查询购销合同对象
        Contract contract = contractDao.selectByPrimaryKey(extc.getContractId());
        // 5.修改并设置总金额
        contract.setTotalAmount(contract.getTotalAmount() + money);
        // 6.修改并设置附件种类数量
        contract.setExtNum(contract.getExtNum() + 1);
        // 7.更新购销合同对象
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ExtCproduct extc) {
        // 1.根据附件id，查询附件原有总金额
        double oldMoney = extCproductDao.selectByPrimaryKey(extc.getId()).getAmount();
        // 2.计算并设置本次修改后的附件总金额
        double newMoney = 0d;
        if (extc.getPrice() != null && extc.getCnumber() != null) {
            newMoney = extc.getPrice() * extc.getCnumber();
        }
        extc.setAmount(newMoney);
        // 3.更新附件对象
        extCproductDao.updateByPrimaryKeySelective(extc);
        // 4.根据合同id，查询购销合同对象
        Contract contract = contractDao.selectByPrimaryKey(extc.getContractId());
        // 5.修改并设置合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - oldMoney + newMoney);
        // 6.更新购销合同对象
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        //  1.根据附件id，查询附件对象总金额
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        double deleteMoney = extCproduct.getAmount();
        //  2.删除附件
        extCproductDao.deleteByPrimaryKey(id);
        //  3.根据合同id，查询购销合同对象
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //  4.修改并设置合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - deleteMoney);
        //  5.修改并设置附件种类数量
        contract.setExtNum(contract.getExtNum() - 1);
        //  6.更新购销合同对象
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ExtCproduct> findAll(ExtCproductExample example) {
        return extCproductDao.selectByExample(example);
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, ExtCproductExample example) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list);
    }
}
